<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>仓库设置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <h1>采购入库列</h1>
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="入库单号">
        <el-input size="small" clearable v-model="formInline.warehouseCode" placeholder="入库单号"></el-input>
      </el-form-item>
      <el-form-item label="关联采购订单">
        <el-input size="small" clearable v-model="formInline.warehouseName" placeholder="关联采购订单"></el-input>
      </el-form-item>
      <el-form-item label="入库单状态">
        <el-input size="small" clearable v-model="formInline.warehouseName" placeholder="入库单状态"></el-input>
      </el-form-item>
      <br>
      <el-form-item label="收货仓库">
        <el-input size="small" clearable v-model="formInline.warehouseName" placeholder="入库单状态"></el-input>
      </el-form-item>
      <el-form-item label="物流单号">
          <el-input size="small" clearable v-model="formInline.warehouseName" placeholder="入库单状态"></el-input>
      </el-form-item>
      <el-form-item label="入库时间">
          <template>
              <el-date-picker
                class="custom-time-picker"
                size="small"
                v-model="value1"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
          </template>
      </el-form-item>
      <br>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd()">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-dropdown size="small">
          <el-button size="small">
            更多菜单<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>批量取消入库</el-dropdown-item>
            <el-dropdown-item>批量</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="handleSelectionChange" :data="listData" highlight-current-row
              v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="warehouseId" label="仓库ID">
      </el-table-column>
      <el-table-column sortable prop="warehouseCode" label="仓库编码">
      </el-table-column>
      <el-table-column sortable prop="warehouseName" label="仓库名称">
      </el-table-column>
      <el-table-column sortable prop="location" label="位置">
      </el-table-column>
      <el-table-column sortable prop="area" label="面积">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="toHouseArea(scope.row)">库区</el-button>
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" v-model="pageparm" @callFather="callFather"></Pagination>
    <!-- 添加/编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="仓库编码" prop="warehouseCode">
          <el-input size="small" v-model="editForm.warehouseCode" auto-complete="off" placeholder="请输入仓库编码">
            <template slot="prepend">WAREHOUSE_CODE_</template>
          </el-input>
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input size="small" v-model="editForm.warehouseName" auto-complete="off" placeholder="请输入仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input size="small" v-model="editForm.location" auto-complete="off" placeholder="请输入仓库位置"></el-input>
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input size="small" v-model.number="editForm.area" auto-complete="off" placeholder="请输入面积">
            <template slot="append">平方</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "../../components/Pagination";

export default {
  name: "purchaseEnWarehouse",
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        warehouseName: '',
        warehouseCode: '',
        area: '',
        location: '',
      },
      // rules表单验证
      rules: {
        warehouseName: [
          {required: true, message: '请输入仓库名', trigger: 'blur'},
          {min: 2, message: '长度2个字符以上', trigger: 'blur'}
        ],
        warehouseCode: [
          {required: true, message: '请输入编码', trigger: 'blur'},
        ]
      },
      // 搜索表单数据
      formInline: {
        pageNum: 1,
        pageSize: 10,
        warehouseCode: '',
        warehouseName: ''
      },
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      // 批量删除用户存储id的数组
      multipleSelection: []
      // 库区选择
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },
  methods: {
    toHouseArea(row){

      this.$router.push("/index/wmStorage/wm/location/"+row.warehouseId);
    },
    getdata(formInline) {
      console.log(formInline)
      wmList(formInline)
        .then(res => {
          this.loading = false
          if (res.code != 20000) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            const pageInfo = res.data;
            this.listData = pageInfo.list;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum
            this.pageparm.pageSize = this.formInline.pageSize
            this.pageparm.total = pageInfo.total
            console.log(this.pageparm)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('仓库信息加载失败，请稍后再试！')
        })
    },
    callFather(pageparam) {
      this.formInline.pageNum = pageparam.pageNum;
      this.formInline.pageSize = pageparam.pageSize;
      this.getdata(this.formInline)
    },
    search() {
      // console.log(this.formInline)
      this.getdata(this.formInline)
    },
    handleAdd() {
      this.editFormVisible = true;
      this.title = "添加";
      // 表单置空
      this.editForm = {}
    },
    closeDialog() {
      this.editFormVisible = false;
      this.getdata(this.formInline)
    },
    submitForm(editForm) {
      // 点击提交按钮会校验输入框
      this.$refs[editForm].validate((valid) => {
        // 如果校验通过再发请求
        if (valid) {
          if (this.title == "添加") {
            // 实现添加
            wmSave(this.editForm).then(res => {
              if (res.code = 20000) {
                this.editFormVisible = false;
                this.$message.success({
                  message: '仓库添加成功',
                  duration: 500
                })
                this.getdata(this.formInline)
              } else {
                this.$message.error({
                  message: '仓库添加失败',
                  duration: 500
                })
              }
            })
          } else if (this.title = "编辑") {
            console.log(this.editForm)
            userEdit(this.editForm).then(res => {
              if (res.code = 20000) {
                this.editFormVisible = false;
                this.$message.success({
                  message: '仓库更新成功',
                  duration: 500
                })
                this.getdata(this.formInline)
              } else {
                this.$message.error({
                  message: '用户更新失败',
                  duration: 500
                })
              }
            })
          }

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleEdit(index, rowData) {
      // console.log(index,rowData)
      this.title = "编辑"
      this.editForm = rowData;
      this.editFormVisible = true;
    },
    deleteUser(userId) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userDelete(userId).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getdata(this.formInline)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val) {
      // val是数组,其中是前端框架自己封装的,其实是勾选的每一行数据对象
      // console.log(val)
      var idArr = [];
      for (var i = 0; i < val.length; i++) {
        var obj = val[i];
        //console.log(obj)
        idArr.push(obj.userId);
      }
      // console.log(idArr)

      this.multipleSelection = idArr;
    },
    handleBatchDel() {
      this.$confirm('批量删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userBatchDel({
          list:this.multipleSelection
        }).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.formInline.pageNum = 1;
            this.getdata(this.formInline)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>
.custom-time-picker {
  width: 200px; /* 设置时间选择器中每个输入框的宽度（可能需要调整） */
}
</style>
